package com.coocaa.ops.admin.v1.audit.provider.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.coocaa.ops.admin.base.AbstractBizImpl;
import com.coocaa.ops.admin.v1.audit.model.CheckInfoModel;
import com.coocaa.ops.admin.v1.audit.provider.AbstractAuditTaskProviderImpl;
import com.coocaa.ops.admin.v1.module.biz.impl.ModuleInfoBizImpl;
import com.coocaa.ops.admin.v1.module.service.impl.ModuleInfoQueryServiceImpl;
import com.coocaa.ops.common.core.base.model.ResourceBaseModel;
import com.coocaa.ops.common.core.base.util.ResourceStatusValidator;
import com.coocaa.ops.common.core.biz.homepage.entity.HomeModuleContentConfigInfo;
import com.coocaa.ops.common.core.biz.module.entity.ModuleInfoEntity;
import com.coocaa.ops.common.core.biz.module.entity.ModuleTabContentConfigInfo;
import com.coocaa.ops.common.core.biz.tab.entity.TabInfoEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author bijiahao
 * @description
 * @date 2020/6/24 11:37
 */
@Service
public class ModuleAuditTaskProviderImpl extends AbstractAuditTaskProviderImpl<ModuleInfoEntity> {
    @Autowired
    private ModuleInfoBizImpl moduleInfoBiz;
    @Autowired
    private ModuleInfoQueryServiceImpl moduleInfoQueryService;

    @Override
    public AbstractBizImpl getAbstractBizImpl() {
        return moduleInfoBiz;
    }

    @Override
    public CheckInfoModel checkBeforeAudit(ResourceBaseModel resourceBaseModel) {
        ModuleInfoEntity entity = moduleInfoQueryService.getDetailInfo(resourceBaseModel.getId(), resourceBaseModel.getVersion());
        TabInfoEntity topTabInfo = entity.getTopTabInfo();
        CheckInfoModel checkInfoModel = this.validCheckTab(topTabInfo);
        if (checkInfoModel.isResult()) {
            //存在没有审核通过的版面
            return checkInfoModel;
        }
        TabInfoEntity bottomTabInfo = entity.getBottomTabInfo();
        checkInfoModel = this.validCheckTab(bottomTabInfo);
        if (checkInfoModel.isResult()) {
            //存在没有审核通过的版面
            return checkInfoModel;
        }

        List<ModuleTabContentConfigInfo> topTabDmpList = entity.getTopTabDmpList();
        checkInfoModel = this.validCheckTabList(topTabDmpList);
        if (checkInfoModel.isResult()) {
            //存在没有审核通过的版面
            return checkInfoModel;
        }
        List<ModuleTabContentConfigInfo> bottomTabDmpList = entity.getBottomTabDmpList();
        checkInfoModel = this.validCheckTabList(bottomTabDmpList);
        if (checkInfoModel.isResult()) {
            //存在没有审核通过的版面
            return checkInfoModel;
        }
        return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
    }

    /*************** ===私有方法=== ********************* ===start=== ***************************************/

    private CheckInfoModel validCheckTab(TabInfoEntity entity) {
        if (null != entity && ResourceStatusValidator.isNotOnline(entity.getStatus())) {
            //存在没有审核通过的板块
            return CheckInfoModel.getCheckInfoModel(Boolean.TRUE, getCheckMsg(entity));
        }
        return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
    }

    private CheckInfoModel validCheckTabList(List<ModuleTabContentConfigInfo> tabList) {
        if (CollectionUtils.isEmpty(tabList)) {
            return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
        }
        for (ModuleTabContentConfigInfo moduleTabContentConfigInfo : tabList) {
            //存在没有审核通过的板块
            if (null != moduleTabContentConfigInfo && ResourceStatusValidator.isNotOnline(moduleTabContentConfigInfo.getStatus())) {
                return CheckInfoModel.getCheckInfoModel(Boolean.TRUE, getCheckMsg(moduleTabContentConfigInfo));
            }
        }
        return CheckInfoModel.getCheckInfoModel(Boolean.FALSE, null);
    }


    /**
     * 获取校验信息-首页中的版面
     *
     * @param
     * @return
     */
    private String getCheckMsg(TabInfoEntity entity) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("panelID: [")
                .append(entity.getId())
                .append("],")
                .append("panelName： ")
                .append(entity.getTabName())
        ;
        return stringBuilder.toString();
    }
}
